package com.youlu.campus.web.file;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.base.sys.QiniuImageInfo;
import com.youlu.campus.base.user.ImageWhiteAuth;
import com.youlu.campus.component.QiniuConfig;
import com.youlu.campus.component.QiniuUtil;
import com.youlu.campus.entity.SystemApolloConfig;
import com.youlu.campus.service.system.SystemApolloConfigService;
import com.youlu.campus.web.common.utils.IdGenerator;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Service
public class FileUploadService {

    @Autowired
    private QiniuConfig qiniuConfig;

    @Autowired
    private QiniuUtil qiniuUtil;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SystemApolloConfigService systemApolloConfigService;


    public String uploadFile(MultipartFile file) {
        log.info("上传图片开始。。。 {}", file.getOriginalFilename());
        String url = qiniuConfig.getDomainOfBucket();
        String fileName = "img" + IdGenerator.generateId();
        try {
            //上传到七牛
            qiniuUtil.uploadFile(file.getInputStream(), fileName);
            url = url + "/" + fileName;
//            Config config = ConfigService.getAppConfig();
//            String cnf = config.getProperty("image.convert.whitelist", "{}");
//            ImageWhiteAuth whiteAuth = JSON.parseObject(cnf, ImageWhiteAuth.class);
            SystemApolloConfig systemApolloConfig = systemApolloConfigService.find();
            ImageWhiteAuth whiteAuth = systemApolloConfig.getImageWhiteAuth();
            if (whiteAuth.getOpened() && whiteAuth.getWhiteList().contains(file.getContentType())) {
                // 查看图片内容
                ResponseEntity<QiniuImageInfo> re = restTemplate.getForEntity(url + "?imageInfo", QiniuImageInfo.class);
                if (re.getBody() != null && "heic".equals(re.getBody().getFormat())) {
                    url = url + "?imageMogr2/format/jpg";
                }
            }

        } catch (IOException e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            throw new BusinessException("文件上传失败！");
        }
        log.info("上传图片结束 {}", url);
        return url;
    }

    public boolean checkFileName(String fileName) {
        log.info(":>>> 文件名:{}", fileName);
        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        if (fileName.contains(".txt") || fileName.contains(".html") || fileName.contains(".js") ||
                fileName.contains(".java") || fileName.contains(".jar") || fileName.contains(".htm") ||
                fileName.contains(".mht") || fileName.contains(".asp") || fileName.contains(".jsp") ||
                fileName.contains(".phtml") || fileName.contains(".php3")|| fileName.contains(".dat")|| fileName.contains(".exe")||
                fileName.contains(".sh")) {
            log.info(":>>> 文件名:{} 包含非法格式", fileName);
            return false;
        }
        return true;
    }
}
